package com.google.code.mircle.core.transfers;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.io.FileUtils;

import com.google.code.mircle.core.Core;
import com.google.code.mircle.core.Plugin;
import com.google.code.mircle.core.Worker.DownloadState;

public class VGetDownloadPanel extends JPanel {

    Core core;
    Plugin plugin;

    Transfer t;

    JLabel lblNewLabel_1 = new JLabel("false");
    JLabel lblNewLabel_3 = new JLabel("New label");
    JLabel lblNewLabel_4 = new JLabel("New label");
    JButton btnDeleteFile = new JButton("Delete File");
    JButton btnDownloadAgain = new JButton("Download Again");
    private final JLabel lblQuality = new JLabel("Quality");
    private final JLabel lblNewLabel_5 = new JLabel("New label");
    GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
    private final JLabel lblWeb = new JLabel("Web");
    private final LinkLabel lblWeb_1 = new LinkLabel("Web URL");
    private final JLabel lblCurrent = new JLabel("Current");
    private final JLabel lblTotal = new JLabel("Total");
    private final JLabel lblCurrentValue = new JLabel("New label");
    private final JLabel lblTotalValue = new JLabel("New label");

    public VGetDownloadPanel(Core c, Plugin p) {
        this.core = c;
        this.plugin = p;

        GridBagLayout gridBagLayout = new GridBagLayout();
        setLayout(gridBagLayout);

        JLabel lblDirectDownload = new JLabel();
        lblDirectDownload.setText("YouTube Download");
        GridBagConstraints gbc_lblDirectDownload = new GridBagConstraints();
        gbc_lblDirectDownload.insets = new Insets(0, 0, 5, 5);
        gbc_lblDirectDownload.anchor = GridBagConstraints.NORTHWEST;
        gbc_lblDirectDownload.gridx = 0;
        gbc_lblDirectDownload.gridy = 0;
        add(lblDirectDownload, gbc_lblDirectDownload);

        btnDownloadAgain.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                t.stop();
                FileUtils.deleteQuietly(t.getWorker().getTarget());
                t.start();
            }
        });
        GridBagConstraints gbc_btnDownloadAgain = new GridBagConstraints();
        gbc_btnDownloadAgain.insets = new Insets(0, 0, 5, 5);
        gbc_btnDownloadAgain.gridx = 1;
        gbc_btnDownloadAgain.gridy = 0;
        add(btnDownloadAgain, gbc_btnDownloadAgain);

        btnDeleteFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FileUtils.deleteQuietly(t.getWorker().getTarget());
            }
        });
        GridBagConstraints gbc_btnDeleteFile = new GridBagConstraints();
        gbc_btnDeleteFile.insets = new Insets(0, 0, 5, 0);
        gbc_btnDeleteFile.gridx = 2;
        gbc_btnDeleteFile.gridy = 0;
        add(btnDeleteFile, gbc_btnDeleteFile);

        JLabel lblNewLabel = new JLabel("Done?:");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.anchor = GridBagConstraints.NORTHWEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 1;
        add(lblNewLabel, gbc_lblNewLabel);

        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1.gridx = 1;
        gbc_lblNewLabel_1.gridy = 1;
        add(lblNewLabel_1, gbc_lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("targetFile");
        GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
        gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_2.gridx = 0;
        gbc_lblNewLabel_2.gridy = 2;
        add(lblNewLabel_2, gbc_lblNewLabel_2);

        GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
        gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_3.gridx = 1;
        gbc_lblNewLabel_3.gridy = 2;
        add(lblNewLabel_3, gbc_lblNewLabel_3);

        JLabel lblTitle = new JLabel("Title");
        GridBagConstraints gbc_lblTitle = new GridBagConstraints();
        gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
        gbc_lblTitle.gridx = 0;
        gbc_lblTitle.gridy = 3;
        add(lblTitle, gbc_lblTitle);

        GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
        gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_4.gridx = 1;
        gbc_lblNewLabel_4.gridy = 3;
        add(lblNewLabel_4, gbc_lblNewLabel_4);

        GridBagConstraints gbc_lblQuality = new GridBagConstraints();
        gbc_lblQuality.insets = new Insets(0, 0, 5, 5);
        gbc_lblQuality.gridx = 0;
        gbc_lblQuality.gridy = 4;
        add(lblQuality, gbc_lblQuality);

        gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_5.gridx = 1;
        gbc_lblNewLabel_5.gridy = 4;
        add(lblNewLabel_5, gbc_lblNewLabel_5);

        GridBagConstraints gbc_lblWeb = new GridBagConstraints();
        gbc_lblWeb.insets = new Insets(0, 0, 5, 5);
        gbc_lblWeb.gridx = 0;
        gbc_lblWeb.gridy = 5;
        add(lblWeb, gbc_lblWeb);

        GridBagConstraints gbc_lblWeb_1 = new GridBagConstraints();
        gbc_lblWeb_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblWeb_1.gridx = 1;
        gbc_lblWeb_1.gridy = 5;
        add(lblWeb_1, gbc_lblWeb_1);

        GridBagConstraints gbc_lblCurrent = new GridBagConstraints();
        gbc_lblCurrent.insets = new Insets(0, 0, 5, 5);
        gbc_lblCurrent.gridx = 0;
        gbc_lblCurrent.gridy = 6;
        add(lblCurrent, gbc_lblCurrent);

        GridBagConstraints gbc_lblCurrentValue = new GridBagConstraints();
        gbc_lblCurrentValue.insets = new Insets(0, 0, 5, 5);
        gbc_lblCurrentValue.gridx = 1;
        gbc_lblCurrentValue.gridy = 6;
        add(lblCurrentValue, gbc_lblCurrentValue);

        GridBagConstraints gbc_lblTotal = new GridBagConstraints();
        gbc_lblTotal.insets = new Insets(0, 0, 0, 5);
        gbc_lblTotal.gridx = 0;
        gbc_lblTotal.gridy = 7;
        add(lblTotal, gbc_lblTotal);

        GridBagConstraints gbc_lblTotalValue = new GridBagConstraints();
        gbc_lblTotalValue.insets = new Insets(0, 0, 0, 5);
        gbc_lblTotalValue.gridx = 1;
        gbc_lblTotalValue.gridy = 7;
        add(lblTotalValue, gbc_lblTotalValue);

    }

    public void setTransfer(Transfer t) {
        this.t = t;

        VGetDownload dd = (VGetDownload) t.getWorker();

        if (dd.getException() != null)
            lblNewLabel_1.setText(dd.getException().toString());
        else
            lblNewLabel_1.setText("" + (dd.state.downloadState == DownloadState.DOWNLOAD_COMPLETE));

        lblNewLabel_4.setText(dd.getState().info.getTitle());

        if (dd.getState().targetFile != null)
            lblNewLabel_3.setText(dd.getState().targetFile.toString());
        else
            lblNewLabel_3.setText("");

        btnDownloadAgain.setEnabled(!t.isAlive());
        if (dd.getTarget() != null)
            btnDeleteFile.setEnabled(dd.getTarget().exists());
        else
            btnDeleteFile.setEnabled(false);

        if (dd.getState().info.getVq() != null)
            lblNewLabel_5.setText(dd.getState().info.getVq().toString());
        else
            lblNewLabel_5.setText("");

        lblWeb_1.setText(dd.getSource());

        lblCurrentValue.setText(Long.toString(dd.getState().info.getInfo().getCount()));
        lblTotalValue.setText(Long.toString(dd.getState().info.getInfo().getLength()));
    }
}
